package server.model.userModel;

import org.apache.commons.dbutils.handlers.BeanHandler;
import server.db.DBDriver;
import server.db.DBFactory;
import server.entity.Code;
import server.entity.User;
import server.model.factory.ModelFactory;
import server.util.InfoUtils.Info;
import server.util.InfoUtils.InfoEnum;
import server.util.InfoUtils.InfoUtil;
import server.util.JWTUtils;
import server.util.MyUtils;

import java.util.ArrayList;
import java.util.List;

public class UserModel {
    public Info login(Code code) {
        Info info = null;
        String sql = "select * from code where user_name = ? and password = ? limit 1";

        List<Object> params = new ArrayList<>();
        if (MyUtils.NOTNULL(code.getUserName(), code.getPassword())) {
            params.add(code.getUserName());
            params.add(code.getPassword());
        } else {
            return info = InfoUtil.getInfo(InfoEnum.PARAMS_NULL);
        }
        User re_user = DBFactory.getDBDriver().query(sql, new BeanHandler<>(User.class, DBDriver.getProcess()), params.toArray());

        if (re_user != null) {
            //生成token
            String token = JWTUtils.sign(re_user.getUserId());
            if (ModelFactory.getTokenModel().setToken(re_user.getUserId(), token) > 0) {
                info = InfoUtil.getInfo(token);
                return info;
            }
            //设置token错误,返回fail info
            info = InfoUtil.getInfo(InfoEnum.FAIL);
        } else info = InfoUtil.getInfo(InfoEnum.LOGIN_FAIL);
        return info;
    }

    public Info register(Code user) {
        Info info = null;
        String sql = "insert into code (user_name,password) values (?,?)";

        if (!MyUtils.NOTNULL(user.getUserName(), user.getPassword())) {
            return InfoUtil.getInfo(InfoEnum.PARAMS_NULL);
        }

        if (judgeUserName(user.getUserName())) {
            //line为操作数据库影响的行数
            int line = DBFactory.getDBDriver().update(sql, user.getUserName(), user.getPassword());
            if (line > 0) {
                Integer userId = getUserIdInCode(user.getUserName());
                if (userId != null) {
                    if (insert2User(userId, user.getUserName()) > 0) {
                        return InfoUtil.getInfo(InfoEnum.SUCCESS);
                    }
                }
            }
            info = InfoUtil.getInfo(InfoEnum.FAIL);
        } else {
            info = InfoUtil.getInfo(1, "用户名已存在，起名真难，嘿嘿。。");
        }
        return info;
    }

    //退出登录
    public Info logout(int userId) {
        Info info = null;
        int line = ModelFactory.getTokenModel().setToken(userId, "&*&[invalid]&*&");
        if (line > 0) {
            info = InfoUtil.getInfo(InfoEnum.SUCCESS);
        } else {
            info = InfoUtil.getInfo(InfoEnum.FAIL);
        }
        return info;
    }

    //注销账号
    public Info deleteAccount(int userId) {
        String sql = "delete from code where user_id = ? limit 1";
        int line = DBFactory.getDBDriver().update(sql, userId);
        if (line > 0) {
            return InfoUtil.getInfo(InfoEnum.SUCCESS);
        } else {
            return InfoUtil.getInfo(InfoEnum.FAIL);
        }
    }

    /**
     * 判断是否存在相同的用户名
     *
     * @param userName 用户名
     * @return 存在相同用户名返回false，不存在返回true
     */
    public boolean judgeUserName(String userName) {
        String sql = "select user_name from code where user_name = ?";

        User user = DBFactory.getDBDriver().query(sql, new BeanHandler<>(User.class, DBDriver.getProcess()), userName);

        if (user == null) {
            return true;
        } else return false;
    }

    //在code数据表里通过user_name查询user_id
    public Integer getUserIdInCode(String userName) {
        String sql = "select user_id from code where user_name = ? limit 1";
        User user = DBFactory.getDBDriver().query(sql, new BeanHandler<>(User.class, DBDriver.getProcess()), userName);
        return user.getUserId();
    }

    //将user插入user数据表
    public int insert2User(Integer userId, String userName) {
        final String def = "[]";
        String sql = "insert into user (user_id,user_name,cart,bought,store_collection,good_collection) values (?,?,'[]','[]','[]','[]')";
        Integer line = DBFactory.getDBDriver().update(sql, userId, userName);
        return line;
    }

    //修改密码
    public Info changePassword(int userId, String p1, String p2) {
        Info info = null;
        String sql = "update code set password = ? where user_id = ? and password = ?";

        if (!MyUtils.NOTNULL(p1, p2)) {
            return InfoUtil.getInfo(InfoEnum.PARAMS_NULL);
        }

        int line = DBFactory.getDBDriver().update(sql, p2, userId, p1);

        if (line > 0) {
            return InfoUtil.getInfo(InfoEnum.SUCCESS);
        } else {
            info = InfoUtil.getInfo(InfoEnum.FAIL);
        }

        return info;
    }

}
